var http = createRequestObject();
var field = '';
var word ='';
var type='';
var myplayer = '';

function createRequestObject() {
        var ajax = null;
        if (window.XMLHttpRequest) {
                try {
                        ajax = new XMLHttpRequest();
                }
                catch(e) {}
        }
        else if (window.ActiveXObject) {
                try {
                        ajax = new ActiveXObject("Msxm12.XMLHTTP");
                }
                catch (e){
                        try{
                                ajax = new ActiveXObject("Microsoft.XMLHTTP");
                        }
                        catch (e) {}
                }
        }
        return ajax;
}

function killErrors() {
return true;
}
window.onerror = killErrors;

// + ----------------------------- +
// |        BEGIN Rating		   |
// + ----------------------------- +

function Rating(multi_id,media_id,star) {
	try {
		document.getElementById("rate_s").innerHTML = loadingText;
		document.getElementById("rate_s").style.display = "block";
		hide_rating_process();
		http.open('POST',  web_url+'/index.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = function() {
			if((http.readyState == 4)&&(http.status == 200)){
				document.getElementById("rating_field").innerHTML = http.responseText;
			}
		}
	http.send('ajaxtype=rating&multi_id='+multi_id+'&media_id='+media_id+'&star='+star);
	}
	catch(e){}
	finally{}
	return false;
}

	// pre-fetch image
	(new Image()).src = RATE_OBJECT_IMG;
	(new Image()).src = RATE_OBJECT_IMG_HALF;
	(new Image()).src = RATE_OBJECT_IMG_BG;

function show_star(starNum,rate_text) {
       	document.getElementById("rate_text_d").innerHTML = rate_text; 
	remove_star();
	full_star(starNum);
}

function full_star(starNum) {
	for (var i=0; i < starNum; i++)
		document.getElementById('star'+ (i+1)).src = RATE_OBJECT_IMG;
}

function remove_star() {
	for (var i=0; i < 5; i++)
		document.getElementById('star' + (i+1)).src = RATE_OBJECT_IMG_BG; // RATE_OBJECT_IMG_REMOVED;
}

function show_rating_process() {
	if(document.getElementById("rating_process")) document.getElementById("rating_process").style.display = "block";
	if(document.getElementById("rate_s")) document.getElementById("rate_s").style.display = "none";
}

function hide_rating_process() {
	if(document.getElementById("rating_process")) document.getElementById("rating_process").style.display = "none";
	if(document.getElementById("rate_s")) document.getElementById("rate_s").style.display = "block";
}
// + ----------------------------- +
// |  		  END Rating		   |
// + ----------------------------- +


function Get_Player(movie_id,epi_id,total,cur) {
	try {
		document.getElementById("load_playing").innerHTML = loadingText;
		document.getElementById("load_playing").style.display = "block";
		http.open('POST',  web_url+'/index.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = function() {
			if((http.readyState == 4)&&(http.status == 200)){
				document.getElementById("load_playing").style.display = "none";
				document.getElementById("playing").innerHTML = http.responseText;
				for (var i=0; i < total; i++) {
					document.getElementById('epi_'+(i+1)).className = 'cl_part';
				}
				document.getElementById('epi_'+cur).className = 'cl_part_cur';
				if(document.getElementById('amung_online')){
					document.getElementById('amung_online').src = 'http://whos.amung.us/swidget/ir53cp3wapxq/000000fff200.png';
				}
			}
		}
	http.send('ajaxtype=player&movie_id='+movie_id+'&epi_id='+epi_id);
	}
	catch(e){}
	finally{}
	return false;
}

// + ----------------------------- +
// |  		  Begin COMMENT		   |
// + ----------------------------- +
function comment_check_values() {
		multi_id = encodeURIComponent(document.getElementById("multi_id").value);
 		media_id = encodeURIComponent(document.getElementById("media_id").value);
 		var comment_content = encodeURIComponent(document.getElementById("comment_content").value);
 if(comment_content == "")
  alert("Báº¡n chÆ°a nháº­p cáº£m nháº­n");
 else {
  try {
   document.getElementById("comment_loading").innerHTML = loadingText;
   document.getElementById("comment_loading").style.display = "block";
   http.open('POST',  web_url+'/index.php');
   http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
   http.onreadystatechange = function() {
			if((http.readyState == 4)&&(http.status == 200)){
				document.getElementById("comment_loading").style.display = "none";
    			document.getElementById("show_list_comment").innerHTML = http.responseText;		
			}
	}
   http.send('ajaxtype=comment&multi_id='+multi_id+'&media_id='+media_id+'&comment_content='+comment_content);
  }
  catch(e){}
  finally{}
 }
 return false;
}
function comment_delete(cm_id,multi_id,media_id) {
 if (confirm("Báº¡n cÃ³ muá»‘n xÃ³a cáº£m nháº­n này khÃ´ng ?")) {
  document.getElementById("comment_loading").innerHTML = loadingText;
  document.getElementById("comment_loading").style.display = "block";
  http.open('POST', web_url+'/index.php');
  http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  http.onreadystatechange = function() {
		if((http.readyState == 4)&&(http.status == 200)){
       		document.getElementById("comment_loading").style.display = "none";
			document.getElementById("show_list_comment").innerHTML = http.responseText;		
		}
  }
  http.send('ajaxtype=comment&type=delete&cm_id='+cm_id+'&multi_id='+multi_id+'&media_id='+media_id);
 }
 return false;
}
function textCounter(field, countfield, maxlimit) {
  if (field.value.length > maxlimit)
      {field.value = field.value.substring(0, maxlimit);}
      else
      {countfield.value = maxlimit - field.value.length;}
}

function change_comment_page(type,num_per,id,page) {
	try {
   document.getElementById("comment_loading").innerHTML = loadingText;
   document.getElementById("comment_loading").style.display = "block";
		http.open('POST',  web_url+'/index.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = function() {
			if((http.readyState == 4)&&(http.status == 200)){
				document.getElementById("comment_loading").style.display = "none";
				document.getElementById("show_list_comment").innerHTML = http.responseText;				
			}
		}
	if(type=="multi")	
		http.send('ajaxtype=comment&type=viewpage&num_per='+num_per+'&multi_id='+id+'&page='+page);
	else if(type=="media")
		http.send('ajaxtype=comment&type=viewpage&num_per='+num_per+'&media_id='+id+'&page='+page);
	}
	catch(e){}
	finally{}
	return false;
}

// + ----------------------------- +
// |  		  End COMMENT		   |
// + ----------------------------- +

function get_download(obj,type,m_id,encrypt_id) {
	try {
		if(obj==''){
			type=encodeURIComponent(type);
			m_id=encodeURIComponent(m_id);
			encrypt_id=encodeURIComponent(encrypt_id);
			multi_id='';
		}
		else{
		var type=encodeURIComponent(type);	
		var m_id=encodeURIComponent(obj.select_down.value);
		var encrypt_id=encodeURIComponent(obj.id_down.value);
		var multi_id=encodeURIComponent(obj.movie_id.value);	
		}
   document.getElementById("field_download").innerHTML = loadingText;
   document.getElementById("field_download").style.display = "block";
		http.open('POST',  web_url+'/index.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = function() {
			if((http.readyState == 4)&&(http.status == 200)){
				document.getElementById("field_download").innerHTML = http.responseText;				
			}
		}
		http.send('ajaxtype=download&type='+type+'&multi_id='+multi_id+'&m_id='+m_id+'&encrypt_id='+encrypt_id);

	}
	catch(e){}
	finally{}
	return false;
}

function change_box_multi_page(type,num_per,id,page) {
	try {
   document.getElementById("pageajax_loading").innerHTML = loadingText;
   document.getElementById("pageajax_loading").style.display = "block";
		http.open('POST',  web_url+'/index.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = function() {
			if((http.readyState == 4)&&(http.status == 200)){
				document.getElementById("pageajax_loading").style.display = "none";
				document.getElementById("show_list_album").innerHTML = http.responseText;				
			}
		}
		http.send('ajaxtype=multimedia&kind=viewpage&type_multi='+type+'&num_per='+num_per+'&id='+id+'&page='+page);

	}
	catch(e){}
	finally{}
	return false;
}

// PLAYER

var playertimeout = 150; //delay 30s
//var wmptimerID;
function delay_getplayer(){
	var timerID = setTimeout("delay_getplayer();", 100);
	if (playertimeout > 0) playertimeout -= 1;
	else{
		clearTimeout(timerID);
		if(myplayer = document.getElementById('myplayer')) {
			myplayer.addModelListener('STATE','auto_change_media_flash');
		}
	}
}

function auto_change_media_flash(obj) {
	if(obj.newstate=='COMPLETED') {
		myplayer.sendEvent('STOP');
		var i=0;
		var check = document.getElementById('chk_media_list');
		var cur = document.getElementById("current_id").value;
		while(cur !=check[i].value && i<(check.length-1)) i++;
		if(check[check.length-1].value == cur) next_id = check[0].value;
		else next_id = check[i+1].value;
		manual_change_media(next_id);
	}
	return false;
}

function manual_change_media(id) {
	try{

		var i=0;
		var x=0;
		var cur = document.getElementById("current_id").value;
		var check = document.getElementById('chk_media_list');
		//clearInterval(wmptimerID);
		while(cur !=check[i].value && i<=(check.length-1)) i++;
		document.getElementById('chk_media_list')[i].selected = '';
		document.getElementById('chk_media_list')[i].style.color = '';
		while(id !=check[x].value && x<=(check.length-1)) x++;
		document.getElementById('chk_media_list')[x].selected = 'selected';
		document.getElementById('chk_media_list')[x].style.color = 'blue';
		document.getElementById("current_id").value = id;
		document.getElementById("playling_loading").innerHTML = loadingText;
		http.open('POST',  web_url+'/index.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        http.onreadystatechange = function() {
            if((http.readyState == 4)&&(http.status == 200)){
				document.getElementById("playling_loading").style.display = "none";
                document.getElementById("media_playing_list").innerHTML = http.responseText;
				playertimeout = 50; //delay 30s
				delay_getplayer();
            }
        }
	    http.send('ajaxtype=qplay&media_id='+id);
	}
	catch(e){}
	finally{}
	return false;	
}

// PLAYLIST

function guest_playlist(media_id,act) {
	try {
	if(!act){
		type="add";
		form_res=document.getElementById("add_pl_loading"+media_id);
	}
	if(act=='1'){
		type="remove";
   		form_res=document.getElementById("remove_pl_loading");
	}
	form_res.innerHTML = loadingText;
   	form_res.style.display = "block";
		http.open('POST',  web_url+'/index.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = function() {
			if((http.readyState == 4)&&(http.status == 200)){
					form_res.style.display = "none";
					document.getElementById("playlist_field").innerHTML = http.responseText;	
			}
		}
		http.send('ajaxtype=playlist&type='+type+'&media_id='+media_id);

	}
	catch(e){}
	finally{}
	return false;
}

function select_playlist(media_id) {
	try {
	form_res=document.getElementById("add_pl_loading"+media_id);
	form_res.innerHTML = loadingText;
   	form_res.style.display = "block";
		http.open('POST',  web_url+'/index.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = function() {
			if((http.readyState == 4)&&(http.status == 200)){
					form_res.innerHTML = http.responseText;		
			}
		}
		http.send('ajaxtype=playlist&media_id='+media_id);
	}
	catch(e){}
	finally{}
	return false;
}

function user_playlist(media_id,act,pl_id) {
	try {
	if(!act){
		type="add";
		s_pl = document.getElementById("select_pl_id");
		pl_id = s_pl.options[s_pl.selectedIndex].value;
		form_res=document.getElementById("remove_pl_loading");
		form_field=document.getElementById("add_pl_loading"+media_id);
	}
	if(act=='1'){
		type="remove";
   		form_res=document.getElementById("remove_pl_loading");
	}
	form_res.innerHTML = loadingText;
   	form_res.style.display = "block";
		http.open('POST',  web_url+'/index.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = function() {
			if((http.readyState == 4)&&(http.status == 200)){
					form_res.style.display = "none";
				if(!act){
					form_field.style.display = "none";
				}
					document.getElementById("playlist_field").innerHTML = http.responseText;	
			}
		}
		if(media_id!=''){
			http.send('ajaxtype=playlist&type='+type+'&media_id='+media_id+'&pl_id='+pl_id);
		}
		else if(pl_id!=''){
			http.send('ajaxtype=playlist&type='+type+'&pl_id='+pl_id);
		}
		else{
			http.send('ajaxtype=playlist&type='+type);
		}

	}
	catch(e){}
	finally{}
	return false;
}

function change_channel(tivi_id,type_tivi) {
	try {
		if(!type_tivi){
			type_tivi=1;	
		}
		document.getElementById("tivi_playing").innerHTML = loadingText;
		document.getElementById("tivi_playing").style.display = "block";
		http.open('POST',  web_url+'/index.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = function() {
			if((http.readyState == 4)&&(http.status == 200)){
				document.getElementById("tivi_playing").innerHTML = http.responseText;
			}
		}
	http.send('ajaxtype=tivi&tivi_id='+tivi_id+'&type_tivi='+type_tivi);
	}
	catch(e){}
	finally{}
	return false;
}



function Get_Player_fullscreen(movie_id,epi_id,total,cur) {
	try {
		document.getElementById("load_playing").innerHTML = loadingText;
		document.getElementById("load_playing").style.display = "block";
		http.open('POST',  web_url+'/index.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = function() {
			if((http.readyState == 4)&&(http.status == 200)){
				document.getElementById("load_playing").style.display = "none";
					var code=http.responseText;
					var w=screen.width-180;
					var h=screen.height-230;
					code=code.replace(/{WIDTH}/g, w);
					code=code.replace(/{HEIGHT}/g, h);
				document.getElementById("playing").innerHTML = code;
				for (var i=0; i < total; i++) {
					document.getElementById('epi_'+(i+1)).className = 'cl_part';
				}
				document.getElementById('epi_'+cur).className = 'cl_part_cur';
			}
		}
	http.send('ajaxtype=player_fullscreen&movie_id='+movie_id+'&epi_id='+epi_id);
	}
	catch(e){}
	finally{}
	return false;
}

function Get_Player_ytb_watch(movie_id,epi_id,total,cur) {
	try {
		document.getElementById("load_playing").innerHTML = loadingText;
		document.getElementById("load_playing").style.display = "block";
		http.open('POST',  web_url+'/index.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = function() {
			if((http.readyState == 4)&&(http.status == 200)){
				document.getElementById("load_playing").style.display = "none";
				document.getElementById("playing").innerHTML = http.responseText;
				for (var i=0; i < total; i++) {
					document.getElementById('epi_'+(i+1)).className = 'cl_part';
				}
				document.getElementById('epi_'+cur).className = 'cl_part_cur';
			}
		}
	http.send('ajaxtype=ytb_watch&movie_id='+movie_id+'&epi_id='+epi_id);
	}
	catch(e){}
	finally{}
	return false;
}



function show_video(video_id) {
	try {
		document.getElementById("video_playing").innerHTML = loadingText;
		document.getElementById("video_playing").style.display = "block";
		http.open('POST',  web_url+'/index.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = function() {
			if((http.readyState == 4)&&(http.status == 200)){
				document.getElementById("video_playing").innerHTML = http.responseText;
			}
		}
	http.send('ajaxtype=video&video_id='+video_id);
	}
	catch(e){}
	finally{}
	return false;
}

function Load_server(server_id,multi_id,total) {
	try {
		document.getElementById("img_load_server").innerHTML = loadingText;
		document.getElementById("img_load_server").style.display = "block";
		http.open('POST',  web_url+'/index.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = function() {
			if((http.readyState == 4)&&(http.status == 200)){
				document.getElementById("img_load_server").style.display = "none";
				document.getElementById("episode_field").innerHTML = http.responseText;
				for (var i=0; i <= total; i++) {
					document.getElementById('server_'+i).className = 'cl_server';
				}
				document.getElementById('server_'+server_id).className = 'cl_server_cur';
				Load_server_download(server_id,multi_id);
			}
		}
	http.send('ajaxtype=load_server_epi&server_id='+server_id+'&multi_id='+multi_id);
	}
	catch(e){}
	finally{}
	return false;
}

function Load_server_download(server_id,multi_id) {
	try {
		document.getElementById("field_server_download").innerHTML = loadingText;
		document.getElementById("field_server_download").style.display = "block";
		http.open('POST',  web_url+'/index.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = function() {
			if((http.readyState == 4)&&(http.status == 200)){
				document.getElementById("field_server_download").innerHTML = http.responseText;
			}
		}
	http.send('ajaxtype=load_server_download&server_id='+server_id+'&multi_id='+multi_id);
	}
	catch(e){}
	finally{}
	return false;
}

function Send_SmS(obj) {
	try {
		sms_num=encodeURIComponent(obj.sms_num.value);
		sms_mes=encodeURIComponent(obj.sms_mes.value);
		document.getElementById("sms_loading").innerHTML = loadingText;
		document.getElementById("sms_loading").style.display = "block";

		http.open('POST',  web_url+'/index.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = function() {
			if((http.readyState == 4)&&(http.status == 200)){
				document.getElementById("sms_loading").innerHTML = http.responseText;
			}
		}
		http.send('ajaxtype=send_sms&sms_num='+sms_num+'&sms_mes='+sms_mes);
	 }

	catch(e){}
	finally{}
	return false;
}

function poptastic(url){
	newwindow=window.open(url,'name','height=675,width=630,left=40,top=75,resizable=no,status=no,scrollbars=no');
	if (window.focus) {newwindow.focus()}
}
